package util;
/**
 * java 与 C++ 以Socket 方式进行数据通信
 */

import java.net.Socket;

public class Communication {
    private byte[] buf = new byte[512]; // 为说明问题，定死大小，事件中可以灵活处理

    /**
     * 将int转为低字节在前，高字节在后的byte数组
     */
    private static byte[] tolh(int n) {
        byte[] b = new byte[4];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        b[2] = (byte) (n >> 16 & 0xff);
        b[3] = (byte) (n >> 24 & 0xff);
        return b;
    }
    /**
     * 将byte数组转化成String
     */
    private static String toStr(byte[] valArr,int maxLen) {
        int index = 0;
        while(index < valArr.length && index < maxLen) {
            if(valArr[index] == 0) {
                break;
            }
            index++;
        }
        byte[] temp = new byte[index];
        System.arraycopy(valArr, 0, temp, 0, index);
        return new String(temp);
    }

    /**
     * 将低字节在前转为int，高字节在后的byte数组
     */
    private static int vtolh(byte[] bArr) {
        int n = 0;
        for(int i=0;i<bArr.length&&i<4;i++){
            int left = i*8;
            n+= (bArr[i] << left);
        }
        return n;
    }
    public String imgAdress = "";
    public int id = 0;
    public String detectionResult = "";
    /**
     * 将float转为低字节在前，高字节在后的byte数组
     */
    private static byte[] tolh(float f) {
        return tolh(Float.floatToRawIntBits(f));
    }

    public static Communication getCommunication(byte[] bArr) {
        String name = "";
        int id = 0;
        String detectionResult = "";

        byte[] temp = new byte[20];
        name = toStr(bArr,20);

        System.arraycopy(bArr, 20, temp, 0, 4);
        id = vtolh(temp);

        System.arraycopy(bArr, 24, temp, 0, 20);
        detectionResult = toStr(temp,20);

        return new Communication(name, id, detectionResult);


    }
    /**
     * 构造并转换
     */
    public Communication(String imgAdress, int id, String detectionResult) {
        this.imgAdress = imgAdress;
        this.id = id;
        this.detectionResult = detectionResult;

        byte[] temp = imgAdress.getBytes();
        System.arraycopy(temp, 0, buf, 0, temp.length);

        temp = tolh(id);
        System.arraycopy(temp, 0, buf, 20, temp.length);

//        temp = tolh(detectionResult);
        temp = detectionResult.getBytes();
        System.arraycopy(temp, 0, buf, 24, temp.length);
    }

    /**
     * 返回要发送的数组
     */
    public byte[] getbuf() {
        return buf;
    }

    /**
     * 发送测试
     */
    public static void main(String[] args) {
        try {
            int index = 0;
            byte[] receive = new byte[512];
            int k=15;
            while(k>0){
                Socket sock = new Socket("127.0.0.1", 8080);
                System.out.println("send content:  name=kingfish  "+"id="+101);
                sock.getOutputStream().write(new Communication("c://kingfish", 101, "198765").getbuf());
                sock.getInputStream().read(receive);
                Communication ee = getCommunication(receive);
                System.out.println("response:imgAdress="+ee.imgAdress+"  "+"id="+ee.id +" detectionResult="+ee.detectionResult);
                k--;
                sock.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
